package com.all.ui;

import java.util.List;
import com.all.widget.receiver.BootReceiver;
import com.all.widget.service.ServiceLife;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServiceActivity extends Activity{

	private Button start;
	private Button stop;
	private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_activity);
        start = (Button) findViewById(R.id.btnstart);
        stop = (Button) findViewById(R.id.btnstop);
        start.setOnClickListener(buttonclick);
        stop.setOnClickListener(buttonclick);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
 OnClickListener buttonclick = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			intent = new Intent(ServiceActivity.this,ServiceLife.class);
			if(view.getId() == R.id.btnstart){
				boolean isStart = isServiceRunning(ServiceActivity.this,ServiceLife.class.getName());
		      
		        if (isStart)  
		        {  
		        	Toast.makeText(ServiceActivity.this, "�����Ѿ�����", Toast.LENGTH_LONG).show();  
		        }  
		        else  
		        {  
		        	startService(intent);
		        	Toast.makeText(ServiceActivity.this, "���������ɹ�", Toast.LENGTH_LONG).show(); 
		        }  
			}else
			{
				stopService(intent);  
				Toast.makeText(ServiceActivity.this, "����ֹͣ", Toast.LENGTH_LONG).show();
			}
		}
	};
	

     public static boolean isServiceRunning(Context mContext,String className) {
         boolean isRunning = false;
         ActivityManager activityManager = (ActivityManager)
         mContext.getSystemService(Context.ACTIVITY_SERVICE);
         List<ActivityManager.RunningServiceInfo> serviceList
         = activityManager.getRunningServices(300);
        if (!(serviceList.size()>0)) {
             return false;
         }
         for (int i=0; i<serviceList.size(); i++) {
             if (serviceList.get(i).service.getClassName().equals(className) == true) {
                 isRunning = true;
                 break;
             }
         }
         return isRunning;
     }
     
     //����һ��������
	 BootReceiver receiver;
     //��̬ע�������
     public void regeditService()
     {
    	 //����һ��������
    	 receiver = new BootReceiver();
    	 //����һ��������
    	 IntentFilter filter = new IntentFilter();
    	 //ע�������
    	 registerReceiver(receiver, filter);
     }
     //���ע��
     @Override  
     protected void onDestroy() {  
         super.onDestroy();  
         //��activity��ɾ���ʱ�����������ע��
         unregisterReceiver(receiver);  
     }  
     
     
     public void send(View view) {  
    	 //ע��һ���㲥���͵�intent
         Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED);  
         intent.putExtra("msg", "hello receiver."); 
         //ͨ��㲥���͵�intent����㲥
         sendBroadcast(intent);  
     }  
}
